package com.demo.study.base.jvm;

import java.util.ArrayList;
import java.util.List;

/**
 * @author baymax
 * @since 2025-07-10
 */
public class T05_ErrorTest {
    public static void main(String[] args) {
        //testStackOverflowError(); // java.lang.StackOverflowError 栈溢出
        //test2OutOfMemoryError(); // java.lang.OutOfMemoryError: Java heap space 堆溢出
    }

    private static void test2OutOfMemoryError() {
        List<String> list = new ArrayList<>();
        String num = "hello";
        while (true) {
            list.add(num);
            num = num + num;
        }
    }

    private static void testStackOverflowError() {
        int count = 1;
        try {
            testCycle();
            count++;
        } catch (Exception e) {
            System.out.println(count);
            throw e;
        }
    }

    private static void testCycle() {
        int a, b;
        testCycle();
    }
}
